#ifndef xpack_lang_cxx_index_of_first_miss
#define xpack_lang_cxx_index_of_first_miss
#pragma push_macro("xuser")
#undef  xuser
#define xuser mixc::lang_cxx::inc
#include"define/base_type.hpp"
#include"lang/cxx.hpp"
#include"macro/xforward.hpp"
#pragma pop_macro("xuser")

namespace mixc::lang_cxx{
    inline uxx index_of_first_miss_core(auto main, auto pattern, auto const & compare){
        if (uxx main_length = main->length; main_length == 0){
            return not_exist;
        }
        else for(uxx i = 0, pattern_length = pattern->length; i < main_length; i++){
            for(uxx j = 0;; j++){
                if (j == pattern_length){
                    return i;
                }

                // 约定:
                // left 为待匹配字符串的字符，right 为匹配参数字符
                if (compare(main[i], pattern[j]) == 0){
                    break;
                }
            }
        }
        return not_exist;
    }

    inline uxx index_of_first_miss_core2(auto main, auto pattern, auto const & compare){
        if (main->length == zero){
            return not_exist;
        }

        for(uxx index = 0, pattern_length = pattern->length; main->length != 0u;){
            for(uxx i = 0;; i++){
                if (i == pattern_length){
                    return index;
                }
                if (uxx item_length = pattern[i]->length; item_length == 0 or main->length < item_length){
                    continue;
                }
                else{
                    auto new_main       = main;
                    new_main->length    = item_length;

                    if (pattern[i].compare(new_main, compare) == 0){
                        main            = main.backward(item_length);
                        index          += item_length;
                        break;
                    }
                }
            }
        }
        return not_exist;
    }



    template<class item_t>
    template<class item_seq_t, class>
    inline uxx cxx<item_t>::index_of_first_miss(item_seq_t const & value, invoke_compare_t compare) const {
        return index_of_first_miss_core(xthe, inc::unified_seq<item_seq_t>(value), compare);
    }

    template<class item_t>
    template<class seq_t, class, class>
    inline uxx cxx<item_t>::index_of_first_miss(seq_t && value, invoke_compare_t compare) const {
        return index_of_first_miss_core2(xthe, inc::unified_seq<seq_t>(xforward(value)), compare);
    }

    template<class item_t>
    inline uxx cxx<item_t>::index_of_first_miss(item_cref_t value, invoke_compare_t compare) const {
        return xthe.index_of_first_miss({ value }, compare);
    }

    template<class item_t>
    inline uxx cxx<item_t>::index_of_first_miss(the_cref_t value, invoke_compare_t compare) const {
        return xthe.index_of_first_miss({ value }, compare);
    }
}

#endif
